//
//  OrderInfoViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/5.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "OrderInfoViewController.h"
#import "OrderInfoTableViewCell.h"
#import "GoodInfoViewController.h"

@interface OrderInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;
@property (nonatomic,strong) UIView *funcView;

@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) NSDictionary *dataDictionary;

@end

//网络
@implementation OrderInfoViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"":@"",
                                 @"":@""};
    [[AFAppDotNetAPIClient sharedClient] POST:@"" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if (State(responseObject)) {
            
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

//UI
@implementation OrderInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"订单详情";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.funcView];
    
    if (self.isInBuyCar) {
        NSMutableArray *array = self.navigationController.viewControllers.mutableCopy;
        for (int i = 0; i<array.count; i++) {
            if ([array[i] isKindOfClass:[GoodInfoViewController class]]) {
                [array removeObjectAtIndex:i];
            }
        }
        self.navigationController.viewControllers = array;
    }
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

- (void)callEvent{
    [self callPhone:@"15641944668"];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OrderInfoTableViewCell class])];
    cell.model = self.model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - set & get

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.rowHeight = 75;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[OrderInfoTableViewCell class] forCellReuseIdentifier:NSStringFromClass([OrderInfoTableViewCell class])];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 32)];
        UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 27)];
        contentView.backgroundColor = [UIColor whiteColor];
        UILabel *contentLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 27)];
        contentLable.font = DEF_FontSize_14;
        
        NSMutableAttributedString *attStr = [NSMutableAttributedString new];
        NSTextAttachment *imgAtt = [NSTextAttachment new];
        imgAtt.image = [UIImage imageNamed:@"money"];
        imgAtt.bounds = CGRectMake(0, -1, 10, 12);
        [attStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:imgAtt]];
        [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"  订单号 %@",self.model.orderNum]]];
        contentLable.attributedText = attStr;
        
        UILabel *stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 27)];
        stateLabel.font = DEF_FontSize_14;
        stateLabel.textAlignment = NSTextAlignmentRight;
        stateLabel.text = @[@"未接单",@"已接单"][[self.model.state intValue]];
        
        [_headerView addSubview:contentView];
        [contentView addSubview:contentLable];
        [contentView addSubview:stateLabel];
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 228)];
        UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, 65)];
        infoView.backgroundColor = [UIColor whiteColor];
        
        NSString *titles = @"下单时间\n产品规格\n产品性能";
//        NSString *infos = @"2017-04-06\n2017-04-06\nxxxxxxxxxxxx\nxxxxxxxxxxxx\nxxxxxxxxxxxx\nxxxxxxxxxxxx";
        NSString *infos = [NSString stringWithFormat:@"%@\n%@\n%@",_model.orderTime,_model.gSpecifications,_model.gProperty];
        UILabel *titlesLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, infoView.frame.size.height)];
        titlesLabel.font = DEF_FontSize_12;
        titlesLabel.numberOfLines = 0;
        
        UILabel *infosLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, infoView.frame.size.height)];
        infosLabel.font = DEF_FontSize_12;
        infosLabel.numberOfLines = 0;
        
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titles];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:4];//调整行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [titles length])];
                titlesLabel.attributedText = attributedString;
        
        NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:infos];
        NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle1 setLineSpacing:4];//调整行间距
        [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [infos length])];
        infosLabel.attributedText = attributedString1;
        infosLabel.textAlignment = NSTextAlignmentRight;

        UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(infoView.frame)+5, SCREEN_WIDTH, _footerView.frame.size.height - CGRectGetMaxY(infoView.frame) - 5)];
        otherView.backgroundColor = [UIColor whiteColor];
        
        UIButton *submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100/WIDTH_5S_SCALE, 32)];
        submitBtn.center = CGPointMake(otherView.frame.size.width/2., otherView.frame.size.height/2.);
        submitBtn.backgroundColor = getColor(redColor);
        submitBtn.layer.masksToBounds = YES;
        submitBtn.layer.cornerRadius = 16;
        submitBtn.titleLabel.font = DEF_FontSize_13;
        [submitBtn setTitle:@"确定" forState:UIControlStateNormal];
        
        [_footerView addSubview:infoView];
        [_footerView addSubview:otherView];
        [infoView addSubview:titlesLabel];
        [infoView addSubview:infosLabel];
        
//        [otherView addSubview:submitBtn];
    }
    return _footerView;
}

- (UIView *)funcView{
    if (!_funcView) {
        _funcView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 64 - 44, SCREEN_WIDTH, 44)];
        _funcView.backgroundColor = getColor(@"3b3b3b");
        
        UIButton *callBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200/WIDTH_5S_SCALE, 30)];
        callBtn.center = CGPointMake(SCREEN_WIDTH/2., _funcView.frame.size.height/2.);
        [callBtn setTitle:[NSString stringWithFormat:@"   联系商家 %@",_model.sellerPhone] forState:UIControlStateNormal];
        callBtn.titleLabel.font = DEF_FontSize_15;
        [callBtn setImage:[UIImage imageNamed:@"phone"] forState:UIControlStateNormal];
        
        [callBtn addTarget:self action:@selector(callEvent) forControlEvents:UIControlEventTouchUpInside];
        [_funcView addSubview:callBtn];
    }
    return _funcView;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[];
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

@end
